package com.yuan.pdftest.test.pdf.controller;

import com.yuan.pdftest.test.pdf.PDFTemplateUtil;
import com.yuan.pdftest.test.pdf.domain.PdfDataTest;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayOutputStream;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
//@RequestMapping("/pdf")
public class PdfController {
    @RequestMapping("/export2")
    public void exportPdf2(HttpServletResponse response) throws Exception {
        Map<String, Object> data = getStringObjectMap();
        String fileName = URLEncoder.encode("xxx报告.pdf", "UTF-8");
        try {
            PDFTemplateUtil.createPDF(data, "pdf测试模板.ftl", fileName, response);
        } catch (Exception e) {
            e.printStackTrace();
            throw new Exception("生成pdf失败：" + e.getMessage());
        }
    }

    private Map<String, Object> getStringObjectMap() {
        // 模板中的数据，实际运用从数据库中查询
        Map<String, Object> data = new HashMap<>();
        data.put("curr", 1);
        data.put("one", 2);
        data.put("two", 1);
        data.put("three", 6);
        List<PdfDataTest> detailList = new ArrayList<>();
        detailList.add(new PdfDataTest(123456, "测试", "测试", "测试", "测试"));
        detailList.add(new PdfDataTest(111111, "测试shi", "测试", "测试", "测试"));
        detailList.add(new PdfDataTest(222222, "测试", "测试", "测试", "测试"));
        data.put("detailList", detailList);
        return data;
    }

    @RequestMapping("/a")
    public void exportPdf(HttpServletResponse response) throws Exception {
        ByteArrayOutputStream baos = null;
        OutputStream out = null;
        try {
            // 模板中的数据，实际运用从数据库中查询
            Map<String, Object> data = new HashMap<>();
            data.put("curr", 1);
            data.put("one", 2);
            data.put("two", 1);
            data.put("three", 6);

            List<PdfDataTest> detailList = new ArrayList<>();
            detailList.add(new PdfDataTest(123456, "不是测试测试", "测试", "测试", "测试"));
            detailList.add(new PdfDataTest(111111, "演戏", "测试", "测试", "测试"));
            detailList.add(new PdfDataTest(222222, "测试", "测试", "测试", "测试"));
            data.put("detailList", detailList);
            System.out.println(data);

            baos = PDFTemplateUtil.createPDF(data, "pdf测试模板.ftl");
            ;
            // 设置响应消息头，告诉浏览器当前响应是一个下载文件
            response.setContentType("application/x-msdownload");
            // 告诉浏览器，当前响应数据要求用户干预保存到文件中，以及文件名是什么 如果文件名有中文，必须URL编码
            String fileName = URLEncoder.encode("月度报告.pdf", "UTF-8");
            response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
            out = response.getOutputStream();
            baos.writeTo(out);
            baos.close();
        } catch (Exception e) {
            e.printStackTrace();
            throw new Exception("导出失败：" + e.getMessage());
        } finally {
            if (baos != null) {
                baos.close();
            }
            if (out != null) {
                out.close();
            }
        }
    }
}